package cn.wolfcode.wolfcodemall.controller.admin;

import cn.wolfcode.wolfcodemall.domain.User;
import cn.wolfcode.wolfcodemall.service.UserService;
import cn.wolfcode.wolfcodemall.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/user")
    public String users(Model model,@RequestParam Map<String,Object> map){
       String name = (String) map.get("name");
        List<User> list;
        System.out.println(map);
       if (StringUtils.hasText(name)) {
           list = userService.selectAll(name);
       } else {
           list = userService.selectAll();
       }

        Map<String, Object> pageResult = new HashMap<>();
        pageResult.put("list", list);
        pageResult.put("total", list.toArray().length);
        model.addAttribute("pageResult", pageResult);

        return "/admin/userManagePage";
    }

    @RequestMapping("/userDetails/{user_id}")
    public String userDetails(@PathVariable("user_id") Integer user_id, Model model){
        System.out.println(user_id);
        User user = userService.selectByUserId(user_id);
        System.out.println(user);
        model.addAttribute("user", user);
        return "/admin/userDetails";
    }

}

